package com.zzj.ecology.framework.interceptor;

import com.zzj.ecology.common.utils.ServletUtils;
import com.zzj.ecology.system.domain.Paging;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @ClassName PaginationInterceptor
 * @Description TODO
 * @Author zhijun zeng at 23:40 2020/5/29
 * @Version 1.0
 **/
@Component
public class PaginationInterceptor extends HandlerInterceptorAdapter {

    public static final String PAGINATION_KEY = "pagination";

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        Integer pageSize = ServletUtils.getParameterToInt("pageSize");
        Integer offset = ServletUtils.getParameterToInt("offset");
        String sort = ServletUtils.getParameter("sort");
        String orderByColumn = ServletUtils.getParameter("orderByColumn");

        Paging paging = new Paging();
        paging.setOffset(offset);
        paging.setPageSize(pageSize);
        paging.setOrderByColumn(orderByColumn);
        paging.setSort(sort);
        request.setAttribute(PAGINATION_KEY, paging);
        return super.preHandle(request, response, handler);
    }
}
